package be.idamf.flash.net
{
	import flash.events.IEventDispatcher;
	import flash.utils.Dictionary;
	import flash.utils.Proxy;
	import flash.utils.flash_proxy;
	
	/**
	 * @author Jan Van Coppenolle
	 */
	dynamic public class NetClientDelegate extends Proxy
	{
		public var dispatcher:IEventDispatcher;
		
		protected var handlers:Dictionary;
		
		////////////////////////////////////////////////////////////////////////
		
		public function NetClientDelegate(dispatcher:IEventDispatcher)
		{
			this.dispatcher = dispatcher;
			this.handlers = new Dictionary();
		}
		
		////////////////////////////////////////////////////////////////////////
		// API
		
		/**
		 * Adds a handler. Note that this is not a weak referece!
		 */
		public function addHandler(netCall:String, functionReference:Function):void
		{
			handlers[netCall] = functionReference;
		}
		
		/**
		 * Removes a handler.
		 */
		public function removeHandler(netCall:String):void
		{
			delete handlers[netCall];
		}
		
		////////////////////////////////////////////////////////////////////////
		// NetStream specific
		
		/**
		 * Invoked when an embedded cue point is reached while playing
		 * a video file.
		 */
		public function onCuePoint(data:Object):void
		{
			dispatch(NetEvent.CUE_POINT, data);
		}
		
		/**
		 * Invoked when Flash Player receives image data as a byte array
		 * embedded in a media file that is playing.
		 */
		public function onImageData(data:Object):void
		{
			dispatch(NetEvent.IMAGE_DATA, data);
		}
		
		/**
		 * Invoked when the application receives descriptive information
		 * embedded in the video being played.
		 */
		public function onMetaData(data:Object):void
		{
			dispatch(NetEvent.METADATA, data);
		}
		
		/**
		 * Invoked when a NetStream object has completely played a stream.
		 */
		public function onPlayStatus(data:Object):void
		{
			dispatch(NetEvent.PLAY_STATUS, data);
		}
		
		/**
		 * Invoked when Flash Player receives text data embedded in a
		 * media file that is playing.
		 */
		public function onTextData(data:Object):void
		{
			dispatch(NetEvent.TEXT_DATA, data);
		}
		
		////////////////////////////////////////////////////////////////////////
		// NetConnection specific
		
		/**
		 * Invoked when the bandwidth detection completed.
		 */
		public function onBWDone():void
		{
			dispatch(NetEvent.BW_DONE);
		}
		
		////////////////////////////////////////////////////////////////////////
		// Overrides
		
		/**
		 * @inheritDoc
		 * @private
		 */
		override flash_proxy function callProperty(methodName:*, ... args):*
		{
			Function(handlers[methodName]).apply(this, args);
		}
		
		override flash_proxy function getProperty(name:*):*
		{
			trace("NetClientDelegate/getProperty(" + name + ")");
		}
		
		////////////////////////////////////////////////////////////////////////
		
		/**
		 * @private
		 */
		protected function dispatch(type:String, data:Object = null):void
		{
			dispatcher.dispatchEvent(new NetEvent(type, data));
		}
	}
}